// import {createStore} from "element-ui/packages/table/src/store/helper";

import axios from "axios";

export default {
    // todo namespace 应该改成namespaced，之后组件中相应的action和mutaion要添加路径
    namespaced: true,
    // state存储全局数据
    state:() => ({
        trainIdSelected: 0,
        trainNameSelected: '',
        linePreSelect: 0,
        lineSelected: 0,
        trainData: [
            {
                id: 0,
                line: null,
                trains: [
                    {
                        id: 0,
                        trainNum: null,
                        score: null,
                        scores4systems: null
                    }]
            }
        ]
    }),
    // mutations用于变更store中的数据
    // 通过mutations集中监控数据变更，利于维护
    // 不要在mutations异步函数
    mutations: {
        changeTrainData(state, trainData) {
            // console.log('changeTrainData被调用');
            state.trainData = trainData;
            // console.log("trainData更改，当前为")
            // console.log(trainData)
        },
        changeTrainScore(state, refs) {
            // console.log("changeTrainScore-mutation被调用");
            state.trainData[state.linePreSelect].trains[refs.trainId].scores4systems = refs.trainScore.scores4systems;
            state.trainData[state.linePreSelect].trains[refs.trainId].scores4systemsHistory = refs.trainScore.scores4systemsHistory;
            // console.log('返回的分数');
            // console.log(refs.trainScore);
            // console.log('当前车辆分数:');
            // console.log(state.trainData[state.lineSelected].trains[refs.trainId].scores4systems);
        },
        changeLine(state, lineId) {
            state.linePreSelect = lineId;
        },
        changeTrain(state, trainId) {
            state.lineSelected = state.linePreSelect;
            state.trainIdSelected = trainId;
            state.trainNameSelected
                = state.trainData[state.lineSelected].trains[state.trainIdSelected].trainNum;
            // console.log("trainNameSelected: " + state.trainNameSelected);
            // console.log('changeTrain=mutaion被调用，更新后的trainNameSelected:', state.trainNameSelected)
        }
    },
    // 异步操作在actions中处理
    // action不能修改state，需通过mutation
    actions: {
        // postTrainScore
        getTrainBaseData({commit, dispatch}) {
            // console.log('getTrainBaseData被调用')
            return axios.get('/healthManage/trainBaseData')
                .then(res => {
                    // console.log('请求车辆数据成功，返回数据为:');
                    // console.log(res.data.result.info);
                    commit('changeTrainData', res.data.result.info);
                    dispatch('changeTrain', 0);
                }).catch(err => {
                    console.log(err);
                });
        },
        postTrainScore({commit}, trainId) {
            // console.log("changeTrainScore-action被调用");
            let data = {'line': this.state.healthManage_trainScore.linePreSelect, 'train': trainId};
            // console.log(this)
            // console.log('postTrainScore将发起post请求，请求参数为：')
            // console.log(data)
            return axios.post('/healthManage/trainScore', data)
                .then(res => {
                    // console.log('分数result')
                    // console.log(res.data.result);
                    commit('changeTrainScore', {trainScore: res.data.result, trainId: trainId});
                }).catch(err => {
                    console.log(err);
                })
        },
        changeTrain({commit, dispatch}, trainId) {
            // axios.get('/healthManage/trainScore')
            //     .then(res => {
            //         commit('changeTrainScore', res.data.result);
            //         commit('changeTrain', trainId);
            //     }).catch(err => {
            //     console.log(err);
            // });
            // console.log("changTrain-action被调用");
            return dispatch('postTrainScore', trainId).then(() => {
                // console.log('changeTrain-action传入changeTrain-mutaion的trainId:', trainId)
                commit('changeTrain', trainId);
            });
        }
    },
    getters: {

    }
};
